<?php
/**
 *
 * @authors Your Name (you@example.org)
 * @date    2017-05-16 16:34:03
 * @version $Id$
 */
	header('Access-Control-Allow-Origin:http://localhost:8080');
	header('Access-Control-Allow-Methods: GET, POST, PUT');
	header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
	header('Access-Control-Allow-Credentials: true');
	header('Content-Type: application/json');
	class Music {
		public function http($url) { //封装HTTP请求
			$refer = "http://music.163.com/";
			$header[] = "Cookie: " . "appver=1.5.0.75771;";
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
			curl_setopt($ch, CURLOPT_REFERER, $refer);
			$output = curl_exec($ch);
			curl_close($ch);
			return $output;
		}
		public function music_search($word, $limit, $type) {
			// s：搜索的内容
			// offset：偏移量（分页用）
			// limit：获取的数量
			// type：搜索的类型
		    $url = "http://music.163.com/api/search/pc";
		    $post_data = array(
		        's' => $word,
		        'offset' => '0',
		        'limit' => $limit || '20',
		        'type' => $type,
		    );
		    $referrer = "http://music.163.com/";
		    $URL_Info = parse_url($url);
		    $values = array();
		    $result = '';
		    $request = '';
		    foreach ($post_data as $key => $value) {
		        $values[] = "$key=" . urlencode($value);
		    }
		    $data_string = implode("&", $values);
		    if (!isset($URL_Info["port"])) {
		        $URL_Info["port"] = 80;
		    }
		    $request .= "POST " . $URL_Info["path"] . " HTTP/1.1\n";
		    $request .= "Host: " . $URL_Info["host"] . "\n";
		    $request .= "Referer: $referrer\n";
		    $request .= "Content-type: application/x-www-form-urlencoded\n";
		    $request .= "Content-length: " . strlen($data_string) . "\n";
		    $request .= "Connection: close\n";
		    $request .= "Cookie: " . "appver=1.5.0.75771;\n";
		    $request .= "\n";
		    $request .= $data_string . "\n";
		    $fp = fsockopen($URL_Info["host"], $URL_Info["port"]);
		    fputs($fp, $request);
		    $i = 1;
		    while (!feof($fp)) {
		        if ($i >= 15) {
		            $result .= fgets($fp);
		        } else {
		            fgets($fp);
		            $i++;
		        }
		    }
		    fclose($fp);
		    return $result;
		}

		public function get_music_info($music_id) { //获取歌曲信息
		    $url = "http://music.163.com/api/song/detail/?id=" . $music_id . "&ids=%5B" . $music_id . "%5D";
		    return $this->http($url);
		}

		public function get_artist_album($artist_id, $limit) { //获取歌手专辑
		    $url = "http://music.163.com/api/artist/albums/" . $artist_id . "?limit=" . $limit;
		    return $this->http($url);
		}

		public function get_album_info($album_id) {	//获取专辑信息
		    $url = "http://music.163.com/api/album/" . $album_id;
		    return $this->http($url);
		}

		public function get_playlist_info($playlist_id) { //获取歌单
		    $url = "http://music.163.com/api/playlist/detail?id=" . $playlist_id;
		    return $this->http($url);
		}

		public function get_music_lyric($music_id) {//获取歌词
		    $url = "http://music.163.com/api/song/lyric?os=pc&id=" . $music_id . "&lv=-1&kv=-1&tv=-1";
		    return $this->http($url);
		}

		public function get_mv_info($music_id) { //获取MV
		    $url = "http://music.163.com/api/mv/detail?id=".$music_id."&type=mp4";
		    return $this->http($url);
		}
	}
	$music = new Music();
	$requset_type = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
	if($requset_type=='POST' && $_POST){
		$request = $_POST;
		$method = $request['method'];
		$data = json_decode($request['data'], true);
		if($method && $data){
			$result = null;
				if ($method === 'get_mv_info') {
					if(!isset($data['id'])){
						$result = array('code'=>'403');
						echo json_encode($result);
						return;
					}
					$result['data'] = $music->get_music_info($data['id']);
					$result['code'] = '200';
				} else if ($method === 'get_artist_album') {
					if(!isset($data['artist_id']) && !isset($data['limit'])){
						$result = array('code'=>'403');
						echo json_encode($result);
						return;
					}
					$result['data'] = $music->get_artist_album($data['artist_id'],$data['limit']);
					$result['code'] = '200';
				} else if ($method === 'get_album_info') {
					if(!isset($data['album_id'])){
						$result = array('code'=>'403');
						echo json_encode($result);
						return;
					}
					$result['data'] = $music->get_album_info($data['album_id']);
					$result['code'] = '200';
				} else if ($method === 'get_playlist_info') {
					if(!isset($data['playlist_id'])){
						$result = array('code'=>'403');
						echo json_encode($result);
						return;
					}
					$result['data'] = $music->get_playlist_info($data['playlist_id']);
					$result['code'] = '200';
				} else if ($method === 'get_music_lyric') {
					if(!isset($data['id'])){
						$result = array('code'=>'403');
						echo json_encode($result);
						return;
					}
					$result['data'] = $music->get_music_lyric($data['id']);
					$result['code'] = '200';
				} else if ($method === 'get_mv_info') {
					if(!isset($data['id'])){
						$result = array('code'=>'403');
						echo json_encode($result);
						return;
					}
					$result['data'] = $music->get_mv_info($data['id']);
					$result['code'] = '200';
				} else if ($method === 'music_search') {
					if(!isset($data['word']) || !isset($data['limit']) || !isset($data['type'])){
						$result = array('code'=>'403');
						echo json_encode($result);
						return;
					}
					$result = $music->music_search($data['word'],$data['limit'],$data['type']);
				} else {
					$result = array('code' =>400);
				}
			echo json_encode($result);
		}
	}else{
		$json = array('code' => 500);
		echo json_encode($json);
	}
